const Information = require("../model/information.model");
const { createRecord } = require("../service/record.service");
// 使用模糊查询需要先引入Op
const seq = require("sequelize");
const Record = require("../model/record.model");
const Op = seq.Op;
class InformationService {
  /**
   * 更新公告浏览记录
   * @param record_id 记录id
   * @param type view:浏览 love:点赞
   * @returns {Promise<boolean>}
   */
  async updateInformationRecord(record_id,type) {
    const updateOption = {}
    if(type === 'view'){
        updateOption.view = seq.literal('view + 1')
    }else if(type === 'love'){
        updateOption.love = seq.literal('love + 1')
    }
    const res = await Record.update(
        {
            ...updateOption
        },
        {
          where: {
            id: record_id
          },
        }
    );
    return res[0] > 0;
  }
  /**
   * 创建
   * @returns {Promise<{code: string, message: string}>}
   * @param information
   */
  async createInformation(information) {
    const { id } = await createRecord();
    const res = await Information.create({ ...information, record_id: id });

    return res.dataValues;
  }

  /**
   * 更新
   * @param id
   * @param information
   * @returns {Promise<boolean>}
   */
  async updateInformation(id, information) {
    const res = await Information.update(information, { where: { id } });
    return res[0] > 0;
  }

  /**
   * 删除
   * @param id
   * @returns {Promise<boolean>}
   */

  async removeInformation(id) {
    const res = await Information.destroy({ where: { id } });
    return res > 0;
  }

  /**
   * 恢复
   * @param id
   * @returns {Promise<boolean>}
   */

  async restoreInformation(id) {
    const res = await Information.restore({ where: { id } });
    return res > 0;
  }

  async findInformation({ pageNum, pageSize, info_title, sort}) {
    const offset = (pageNum - 1) * pageSize;
    const whereOpt1 = {};
    const orderOpt = [];
    info_title &&
      Object.assign(whereOpt1, {
        info_title: {
          [Op.like]: `%${info_title}%`,
        },
      });

    if(sort === 2) {
      orderOpt.push(["record_info","love", "DESC"])
    }else if (sort === 3) {
      orderOpt.push(["record_info","view", "DESC"])
    }else{
      orderOpt.push(["createdAt", "DESC"])
    }

    console.log(orderOpt,"orderOpt")

    const { count, rows } = await Information.findAndCountAll({
      offset: offset,
      limit: pageSize * 1,
      order:orderOpt,
      where: whereOpt1,
      include: [
        {
          model: Record,
          as: "record_info",
        },
      ],
    });

    return {
      pageNum,
      pageSize,
      total: count,
      list: rows,
    };
  }

  async findInformationInfo(id) {
    const whereOpt = {};
    id && Object.assign(whereOpt, { id });

    const res = await Information.findOne({
      where: whereOpt,
      include: [
        {
          model: Record,
          as: "record_info",
        },
      ],
    });
    return res ? res.dataValues : null;
  }
}

module.exports = new InformationService();
